#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
审核结果通知模型
"""

from typing import Dict, Any, Optional, List
from dataclasses import dataclass, asdict
from datetime import datetime


@dataclass
class NotifyParam:
    """通知参数"""
    method: str = ""  # 请求方法
    uri: str = ""  # 请求URI
    alg: str = ""  # 算法类型
    sign: str = ""  # 签名
    nonce: str = ""  # 随机数
    timestamp: str = ""  # 时间戳
    body: str = ""  # 请求体

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return asdict(self)


@dataclass
class AuditDetail:
    """审核详情"""
    field_name: str = ""  # 字段名称
    field_value: str = ""  # 字段值
    error_msg: str = ""  # 错误信息

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return asdict(self)


@dataclass
class ApplyResultRecord:
    """申请结果记录"""
    apply_id: str = ""  # 业务申请编号
    app_id: str = ""  # 机构号
    audit_status: str = ""  # 审核状态 01：审核通过, 02：审核中， 09：审核驳回
    audit_detail: Optional[List[AuditDetail]] = None  # 审核驳回详情
    audit_failure: str = ""  # 审核驳回描述
    merch_no: str = ""  # 商户号
    sign_id: str = ""  # 协议编号
    delegation_sign_id: str = ""  # 委托合同协议编号
    acquiring_sign_id: str = ""  # 收单合同协议编号
    sign_status: str = ""  # 签约状态 00:未签约 01:已签约 02:签约中 09:签约失败
    sign_delegatee_status: str = ""  # 授权收款模式下，被授权人的签约状态
    sign_delegatee_url: str = ""  # 授权收款模式下，被授权人的承诺函、电子协议签约链接
    sign_url: str = ""  # 协议链接
    term_no: str = ""  # 终端号
    audit_end_time: Optional[datetime] = None  # 审核时间
    sub_appid_bind_ret: str = ""  # appid绑定结果 01: 成功 02: 处理中 09: 失败
    sub_appid_bind_msg: str = ""  # 绑定结果描述
    wechat_jsapi_path_ret: str = ""  # 支付目录绑定结果 01: 成功 02: 处理中 09: 失败
    wechat_jsapi_path_msg: str = ""  # 支付目录绑定结果描述
    coop_bank_ret: str = ""  # 入驻银行合作平台 01: 成功 02: 处理中 09: 失败
    coop_bank_msg: str = ""  # 入驻结果描述

    def __post_init__(self):
        """初始化后处理"""
        if self.audit_detail is None:
            self.audit_detail = []

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = asdict(self)
        if self.audit_detail:
            data['audit_detail'] = [detail.to_dict() for detail in self.audit_detail]
        if self.audit_end_time:
            data['audit_end_time'] = self.audit_end_time.strftime("%Y-%m-%d %H:%M:%S")
        return data 